<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
use think\Db;

function is_null_or_empty($value)
{
    if (is_null($value) || $value === "") {
        return true;
    }
    
    return false;
}

function build_order_no()
{
    return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}
/** * 生成32位的唯一字符串 */
function build_order_no32()
{
    return md5(uniqid(mt_rand(), true));
}
function is_mobile($mobile)
{
    if (! is_numeric($mobile)) {
        return false;
    }
    return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
}

function D($tablename)
{
    return Db::name($tablename);
}

function I($param)
{
    return trim(input($param));
}

/**
 * 32位uuid
 *
 * @return string
 */
function uuidBy32()
{
    $charid = md5(uniqid(mt_rand(), true));
    $hyphen = chr(45); // "-"
    $uuid = substr($charid, 0, 8) . $hyphen . substr($charid, 8, 4) . $hyphen . substr($charid, 12, 4) . $hyphen . substr($charid, 16, 4) . $hyphen . substr($charid, 20, 12);
    return str_replace('-', '', $uuid);
}

/**
 * post进行url请求
 *
 * @param string $url            
 * @param array $post            
 */
function request_post($url = '', $post = array())
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HEADER => false,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $post
    );
    $ch = curl_init($url);
    curl_setopt_array($ch, $options);
    $result = curl_exec($ch);
    curl_close($ch);
    $res = json_decode($result, true);
    
    return $res;
}

function request_get($url)
{
    $result = file_get_contents($url);
    $res = json_decode($result, true);
    return $res;
}

function getMillisecond()
{
    list ($s1, $s2) = explode(' ', microtime());
    return (float) sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
}

function saveLog($content, $filename = NULL)
{
    if ($filename == null) {
        $filename = date('Y-m-d', time());
    }
    $path = 'ZLog/';
    $chmod = 777;
    if (! file_exists($path)) {
        @mkdir($path);
        @chmod($path, $chmod);
    }
    $file = 'ZLog/' . $filename . '.txt';
    @file_put_contents($file, date('Y-m-d H:i:s', time()) . $content . "\n\r", FILE_APPEND);
}
function getnowtime(){
    return date("Ymd");
}

function delFileUnderDir($dirName)
{

    if (! is_dir($dirName)) {
        mkdir($dirName, 0777, true);
    }


    if ($handle = opendir("$dirName")) {
        while (false !== ($item = readdir($handle))) {
            if ($item != "." && $item != "..") {
                if (is_dir("$dirName/$item")) {
                    //delFileUnderDir("$dirName/$item");
                } else {
                    if (unlink("$dirName/$item")) {
                        // echo "成功删除文件： $dirName/$item\n";
                    }
                }
            }
        }
        closedir($handle);
    }
}

function array_sort($arrs, $key, $orderby = false)
{
    if (count($arrs)<2){
        return $arrs;
    }
    if (!$orderby) {
        $orderby = "SORT_DESC";
    }else {
        $orderby = "SORT_ASC";
    }
    $sort = array(
        'direction' => $orderby, // 排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
        'field' => $key
    );
    $arrSort = array();
    foreach ($arrs as $uniqid => $row) {
        foreach ($row as $key => $value) {
            $arrSort[$key][$uniqid] = $value;
        }
    }
    if ($sort['direction']) {
        array_multisort($arrSort[$sort['field']], constant($sort['direction']), $arrs);
    }
    return $arrs;
}
function str_get_last($str,$n){
    return substr($str,strlen($str)- $n,1); //总长度减3，得到的长度往后截取3位，得到的就是最后三位
}